package com.ocom.webapi.business.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ocom.common.request.payconfig.AddConfigureInfoRequest;
import com.ocom.common.request.payconfig.DeleteConfigureInfoRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.utils.RequestUtil;
import com.ocom.security.utils.SecurityUtils;
import com.ocom.webapi.business.PayCenterService;
import com.ocom.webapi.feign.PayConfigClientService;
import com.ocom.webapi.po.PayCenterVo;
import com.ocom.webapi.service.CompanyService;
import com.ocom.webapi.vo.PayCenterCompanyVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;


/**
 * 统一支付平台
 */
@RefreshScope
@Service
public class PayCenterServiceImpl implements PayCenterService {

	private Logger logger = LoggerFactory.getLogger(PayCenterServiceImpl.class);

//	@Value("${paycenter.url}")
//	private String payCenterUrl;


	@Autowired
	private PayConfigClientService payConfigClientService;

	@Autowired
	private CompanyService companyService;

	@Override
	public PayCenterVo addMerch(Long comId,PayCenterCompanyVo payCenterCompanyVo) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());

		Map<String,Object> map2=new HashMap<>();
		map2.put("companyName",payCenterCompanyVo.getCompanyName());
		map2.put("contactAddress","鑫澳康");
		map2.put("contactName","云卡通3.1");
		if(!CommonUtil.isNull(payCenterCompanyVo.getContactAddress())){
			map2.put("contactAddress",payCenterCompanyVo.getContactAddress());
		}
		if(!CommonUtil.isNull(payCenterCompanyVo.getContactName())){
			map2.put("contactName",payCenterCompanyVo.getContactName());
		}
		map2.put("contactNumber",payCenterCompanyVo.getContactNumber());
		map2.put("isEverySet",payCenterCompanyVo.getIsEverySet());
		map2.put("locked",payCenterCompanyVo.getLocked());
		map2.put("payChannel","111111");
		map2.put("password",payCenterCompanyVo.getPayPassword());
		map2.put("userName",payCenterCompanyVo.getUserName());
		map.put("company",new Gson().toJson(map2));

	//	System.out.println(JSON.toJSON(map));
		String payCenterUrl = companyService.getPayCenterUrl(comId,null);
		String suty = RequestUtil.doPost(payCenterUrl+"company/add",String.valueOf(JSON.toJSON(map)));

		PayCenterVo payCenterVo=new Gson().fromJson(suty,PayCenterVo.class);

		logger.info("添加商户-统一支付返回  【{}】",payCenterVo);

		return payCenterVo;
	}

	@Override
	public PayCenterVo editMerch(Long comId,PayCenterCompanyVo payCenterCompanyVo) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());

		if(CommonUtil.isNull(payCenterCompanyVo.getCompanyId())){
			logger.info("修改商户异常 -  商户companyId为空 , 参数【{}】" ,payCenterCompanyVo);
		}

		Map<String,Object> map2=new HashMap<>();
		map2.put("companyId",payCenterCompanyVo.getCompanyId());
		map2.put("companyName",payCenterCompanyVo.getCompanyName());
		map2.put("contactAddress","鑫澳康");
		map2.put("contactName","云卡通3.1");
		map2.put("contactNumber",payCenterCompanyVo.getContactNumber());
		map2.put("isEverySet",payCenterCompanyVo.getIsEverySet());
		map2.put("locked",payCenterCompanyVo.getLocked());
		map2.put("payChannel","111111");
//		map2.put("password","123456");
//		map2.put("userName",payCenterCompanyVo.getUserName());
		map.put("company",new Gson().toJson(map2));

	//	System.out.println(JSON.toJSON(map));
		String payCenterUrl = companyService.getPayCenterUrl(comId,payCenterCompanyVo.getCompanyId());
		String suty = RequestUtil.doPost(payCenterUrl+"company/revise",String.valueOf(JSON.toJSON(map)));

		PayCenterVo payCenterVo=new Gson().fromJson(suty,PayCenterVo.class);


		logger.info("请求地址  【{}】",payCenterUrl+"company/revise");
		logger.info("编辑商户信息-统一支付返回  【{}】 【{}】",payCenterVo,suty);

		return payCenterVo;
	}

	@Override
	public PayCenterVo merchInfo(Long comId,String companyId) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());

		map.put("companyId",companyId);

	//	System.out.println(JSON.toJSON(map));
		String payCenterUrl = companyService.getPayCenterUrl(comId, companyId);
		String suty = RequestUtil.doPost(payCenterUrl+"company/select",String.valueOf(JSON.toJSON(map)));

		PayCenterVo payCenterVo=new Gson().fromJson(suty,PayCenterVo.class);

		logger.info("请求地址  【{}】",payCenterUrl+"company/select");
		logger.info("获取商户信息-统一支付返回  【{}】 【{}】",payCenterVo,suty);

		return payCenterVo;
	}

	@Override
	public PayCenterVo addPayChannel(Long comId,AddConfigureInfoRequest savePayChannelRequest,String companyId) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());


		Map<String,Object> map2=new HashMap<>();
		map2.put("companyId",companyId);
		map2.put("payType",savePayChannelRequest.getPayType());

		//获取标识
		String tip;
		try {
			Result result=payConfigClientService.getPageChannelKey(savePayChannelRequest.getPayWay());
			JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(result.getData()));
			tip=jsonObject.getString("keyTip");
		}catch (Exception e){
			return null;
		}
		//wxPayJsonVo
		map2.put(tip,new Gson().fromJson(savePayChannelRequest.getChannelParam(),Map.class));

		map.put("CompanyInfo",map2);
	//	System.out.println(JSON.toJSON(map));
		String payCenterUrl = companyService.getPayCenterUrl(comId, companyId);
		String suty= RequestUtil.doPost(payCenterUrl+"companyInfo/add",String.valueOf(JSON.toJSON(map)));

	//	System.out.println(suty);

		PayCenterVo payCenterVo=new Gson().fromJson(suty,PayCenterVo.class);

		logger.info("请求地址  【{}】",payCenterUrl+"companyInfo/add");
		logger.info("添加支付渠道-统一支付返回  【{}】 【{}】",payCenterVo,suty);

		return payCenterVo;
	}

	@Override
	public PayCenterVo editPayChannel(Long comId,AddConfigureInfoRequest savePayChannelRequest) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());

		Map<String,Object> map2=new HashMap<>();
		map2.put("companyId",savePayChannelRequest.getCompanyId());
//		map2.put("companyId","20220401006");
		map2.put("payType",savePayChannelRequest.getPayType());

		//获取标识
		String tip;
		try {
			Result result=payConfigClientService.getPageChannelKey(savePayChannelRequest.getPayWay());
			JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(result.getData()));
			tip=jsonObject.getString("keyTip");
		}catch (Exception e){
			return null;
		}
		//wxPayJsonVo
		map2.put(tip,new Gson().fromJson(savePayChannelRequest.getChannelParam(),Map.class));

		map.put("CompanyInfo",map2);
	//	System.out.println(JSON.toJSON(map));
	//	System.out.println(JSON.toJSON(map2));
		String payCenterUrl = companyService.getPayCenterUrl(comId, savePayChannelRequest.getCompanyId());
		String suty= RequestUtil.doPost(payCenterUrl+"companyInfo/revise",String.valueOf(JSON.toJSON(map)));

		System.out.println(suty);


		PayCenterVo payCenterVo=new Gson().fromJson(suty,PayCenterVo.class);

		logger.info("请求地址  【{}】",payCenterUrl+"companyInfo/revise");
		logger.info("编辑支付渠道-统一支付返回  【{}】",payCenterVo);

		return payCenterVo;
	}

	@Override
	public PayCenterVo payChannelList(Long comId,String companyId) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());
		map.put("companyId",companyId);

		System.out.println(JSON.toJSON(map));
		String payCenterUrl = companyService.getPayCenterUrl(comId, companyId);
		String suty = RequestUtil.doPost(payCenterUrl+"companyInfo/query",String.valueOf(JSON.toJSON(map)));

		PayCenterVo payCenterVo=new Gson().fromJson(suty,PayCenterVo.class);

		logger.info("获取支付渠道-统一支付返回  【{}】 【{}】",payCenterVo,suty);
		logger.info("请求地址  【{}】",payCenterUrl+"companyInfo/query");

		return payCenterVo;
	}

	@Override
	public PayCenterVo delPayChannel(Long comId,DeleteConfigureInfoRequest deleteConfigureInfoRequest) {
		Map<String,Object> map=new HashMap<>();
		map.put("token","Bearer "+ SecurityUtils.getToken());

		//TODO 获取 payWay
		Result result=payConfigClientService.getDeleteConfigureInfo(deleteConfigureInfoRequest.getComId(),deleteConfigureInfoRequest.getId());

		logger.info("获取 companyId payWay 返回 【{}】",result);
		if(result.getCode()!=200){
			return null;
		}

		JSONObject jsonObject= JSON.parseObject(JSON.toJSONString(result.getData()));

		//TODO 获取companyId
		Result result2=payConfigClientService.MerchPageInfo(deleteConfigureInfoRequest.getComId(),deleteConfigureInfoRequest.getMerchId());
		if(result2.getCode()!=200){
			return null;
		}
		JSONObject jsonObject1= JSON.parseObject(JSON.toJSONString(result2.getData()));
		map.put("companyId",jsonObject1.getString("companyId"));
		map.put("payChannel",jsonObject.getIntValue("payWay"));
		String payCenterUrl = companyService.getPayCenterUrl(comId, jsonObject1.getString("companyId"));
		String suty= RequestUtil.doPost(payCenterUrl+"companyInfo/deletes",String.valueOf(JSON.toJSON(map)));

		PayCenterVo payCenterVo=JSON.parseObject(suty,PayCenterVo.class);

		logger.info("删除支付渠道-统一支付返回 参数【{}】 返回 【{}】 原始 【{}】",map,payCenterVo,suty);
		logger.info("请求地址  【{}】",payCenterUrl+"companyInfo/deletes");

		return payCenterVo;
	}
}
